package com.groupbaby.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.groupbaby.ydd.common.Page;
import com.groupbaby.ydd.common.ResultData;
import com.groupbaby.ydd.entity.YddCate;
import com.groupbaby.ydd.service.IYddCateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/ydd_cate")
@Slf4j
public class YddCateController {

    @Autowired
    private IYddCateService iYddCateService;

    /*
     * GET 标签列表-全部-*/
    @GetMapping("/all")
    public ResultData all() {
        LambdaQueryWrapper<YddCate> queryWrapper = new LambdaQueryWrapper<>();
        List<YddCate> list = iYddCateService.list(queryWrapper);
        return ResultData.ok("成功", list);
    }

    /**
     * 文章详情
     * @param id
     * @return
     */
    @GetMapping("/rowInfo")
    public ResultData getRowInfo(String id){
        int i = Integer.parseInt(id);
        YddCate yddCate = iYddCateService.getById(i);
        return ResultData.ok("",yddCate);
    }


    /**
     *分页对象
     * @param catename
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/list")
    public ResultData list( String catename, String page, String limit){
        log.info("{}-------------{}------------{}",catename,page,limit);
        System.out.println(catename);

        int newPage = Integer.parseInt(page);
        int pageSize = Integer.parseInt(limit);

        com.baomidou.mybatisplus.extension.plugins.pagination.Page pageInfo=new com.baomidou.mybatisplus.extension.plugins.pagination.Page(newPage,pageSize);

        LambdaQueryWrapper<YddCate> lqw=new LambdaQueryWrapper<>();
        if(catename!=""&&catename!=null){
            lqw.like(YddCate::getCatename,catename);
        }

        int count = iYddCateService.count(lqw);

        com.baomidou.mybatisplus.extension.plugins.pagination.Page catePage = iYddCateService.page(pageInfo, lqw);
        List<YddCate> records = catePage.getRecords();

        Page getPage = Page.init( count, pageSize,  newPage, records);

        return ResultData.ok("",getPage);
    }


    /**
     * 增加分类
     * @param yddCate
     * @return
     */
    @PostMapping("/add")
    public ResultData add(@RequestBody YddCate yddCate){
        boolean flag = iYddCateService.save(yddCate);
        return flag?ResultData.ok("添加成功",null):ResultData.error("添加失败");
    }


    /**
     * 修改分类
     * @param yddCate
     * @return
     */
    @PostMapping("/update")
    public ResultData update(@RequestBody YddCate yddCate){
        boolean flag = iYddCateService.updateById(yddCate);
        return flag?ResultData.ok("修改成功",null):ResultData.error("修改失败");
    }


    /**
     *
     * @param id
     * @return
     */
    @GetMapping("/del")
    public ResultData del(String id){
        int i = Integer.parseInt(id);
        boolean flag = iYddCateService.removeById(id);
        return flag?ResultData.ok("删除成功",null):ResultData.error("删除失败");

    }

}
